package com.example.wallpaper.vm;

import android.app.Application;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

public class WallViewVM extends AndroidViewModel {
    private Drawable mDrawable;
    private ContentResolver mContentResolver;
    private Bitmap mBitmap;

    public WallViewVM(@NonNull Application application) {
        super(application);
        mContentResolver = application.getContentResolver();
    }


    public void onSave(){
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DISPLAY_NAME,System.currentTimeMillis()+".jpg");
        values.put(MediaStore.Images.Media.RELATIVE_PATH,"DCIM/wallpaper");
        values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
        Uri uri = mContentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        try {
            OutputStream outputStream = mContentResolver.openOutputStream(uri);
            mBitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Drawable getDrawable() {
        return mDrawable;
    }

    public void setDrawable(Drawable mDrawable) {
        this.mDrawable = mDrawable;
    }

    public void setmBitmap(Bitmap mBitmap) {
        this.mBitmap = mBitmap;
    }
}
